#ifndef UUID_0670950C7F73433084A0A393A01A7E88
#define UUID_0670950C7F73433084A0A393A01A7E88

#include <gvl/support/bits.hpp>
#include <gvl/system/system.hpp>

namespace Psync5
{

struct InitCookie
{
	InitCookie()
	{
	}

	InitCookie(uint64_t salt1, uint64_t salt2)
	{
		init(salt1, salt2, gvl::get_ticks());
	}

	void init(uint64_t salt1, uint64_t salt2, uint32_t timerNew)
	{
		uint64_t a = salt1;
		uint64_t b = salt2;
		timer = timerNew;

		uint64_t dtimer = timer | (uint64_t(timer) << 32);

		for(int i = 0; i < 4; ++i)
		{
			a ^= b;
			b = gvl_rot_64(b, 17);
			a += b;

			b ^= dtimer;
			a += dtimer;

			b ^= a;
			a = gvl_rot_64(a, 27);
			b -= a;
		}

		a ^= salt1;
		b ^= salt2;

		x = a;
		y = b;
	}

	bool verify(uint64_t salt1, uint64_t salt2)
	{
		InitCookie temp;
		temp.init(salt1, salt2, timer);

		return temp.x == x && temp.y == y && temp.timer == timer;
	}

	uint64_t x, y;
	uint32_t timer;
};

} // namespace Psync5

#endif // UUID_0670950C7F73433084A0A393A01A7E88
